﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace ExcelDNAForWPSDemo
{
    internal class Util
    {
        internal static void OutPutError(Exception ex)
        {
            MessageBox.Show($"程序出错，出错原因为:{ex.Message}\n详细错误信息：\n{ex.ToString()}");
        }


        internal static Image GetImageFromResource(string resourceName)
        {
            string resName = GetFullResourceName(resourceName, true);

            if (!string.IsNullOrEmpty(resName))
            {
                var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resName);

                return Image.FromStream(stream);
            }
            else
            {
                return null;
            }


        }

        internal static string GetTextContentsFromResource(string resourceName)
        {
            string resName = GetFullResourceName(resourceName, true);

            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resName);

            return StreamToString(stream);

        }

        internal static string StreamToString(Stream stream)
        {
            using (StreamReader sr = new StreamReader(stream))
            {
                return sr.ReadToEnd();
            }
        }

        internal static string GetFullResourceName(string resourceName, bool isIgnoreCase)
        {
            string resName;
            if (isIgnoreCase)
            {
                resName = Assembly.GetExecutingAssembly().GetManifestResourceNames()
                    .FirstOrDefault(s => s.ToUpper().EndsWith(resourceName.ToUpper()));
            }
            else
            {
                resName = Assembly.GetExecutingAssembly().GetManifestResourceNames()
                    .FirstOrDefault(s => s.EndsWith(resourceName));
            }

            return resName;
        }
    }
}
